package com.settlement.system.controller;

import com.settlement.system.csv.CsvExportBuilder;
import com.settlement.system.csv.CsvExportUtil;
import com.settlement.system.common.enums.ExcelFileType;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.*;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.csv.DateSpiltCsvDatas;
import com.settlement.system.common.util.common.FasUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.settlement.system.service.ITblWpRefundDifAnalysisService;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WP-退供差异分析 前端控制器
 * @author xiaochuan
 * @since 2022-08-30
 */

@Slf4j
@Api(tags = "WP-退供差异分析",description = "WP-退供差异分析")
@RestController
@RequestMapping("/wp/tblWpRefundDifAnalysis")
public class TblWpRefundDifAnalysisController extends ExcelController<TblWpRefundDifAnalysis> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpRefundDifAnalysisService tblWpRefundDifAnalysisService;

    @Override
    public IBaseService init() {
        return tblWpRefundDifAnalysisService;
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpRefundDifAnalysis foot = tblWpRefundDifAnalysisService.getFoot(model);
        result.put("total",foot!=null? foot.getTotal():0);
        result.put("foot",foot);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "poNo", "barCode", "refundNo", "diffType", "sysInitialJudge","followUpDept","companyNo");
        FasUtil.toUpperCase(model, "poNo","barCode","refundNo");
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpRefundDifAnalysis.class;
    }

    @Override
    public void exportExcelPost(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> params) throws Exception {
        setParamsModel(params);
        String fileName = "WP-退供差异分析";
        String path = exportRootPath + System.currentTimeMillis() + File.separator;
        CsvExportUtil.createCsv()
                .headClass(getModelClass())
                .fileName(fileName)
                .fileType(ExcelFileType.XLSX)
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .csvDatas(new DateSpiltCsvDatas(init(), params))
                .export(request, response);
    }

    /*@Override
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) throws ServiceException {
        return super.getPageList2(request,model);
    }*/

    @Override
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) throws ServiceException {
        Map<String, Object> result = new HashMap<>(16);
        try {
            insetMenuMemory(model.getModel());
            processModel(model);
            setParamsModel(model.getModel());
            List<TblWpRefundDifAnalysis> rows = init().selectByPage(new SinglePage(model), model.getModel(),
                    model.getOrderByField(), model.getOrderBy());
            result.put("rows", rows.subList(0,rows.size()-1));
            TblWpRefundDifAnalysis foot = rows.get(rows.size()-1);
            result.put("total",foot!=null? foot.getTotal():0);
            result.put("foot",foot);
        } catch (Exception ex) {
            log.error("获取列表出错",ex);
            return new ObjectDataResponse<Map>(result, ReturnStatusEnum.REQUEST_ABNORMAL_MSG.getValue(), "获取列表出错");
        }
        return new ObjectDataResponse<Map>(result, 200, "获取列表成功");
    }
}

